package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.PublishVideoContract;
import com.sywb.chuangyebao.view.fragment.PublishVideoFromCameraFragment;
import com.sywb.chuangyebao.view.fragment.PublishVideoFromPhotoFragment;

import org.bining.footstone.rxjava.rxbus.RxBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 发布文章
 * Created by duxl on 2018/5/17.
 */

public class PublishVideoActivity extends ActionbarActivity<PublishVideoContract.Presenter> implements PublishVideoContract.View {

    @BindView(R.id.rlBottom_activity_publish_video)
    RelativeLayout mRlBottom;
    @BindView(R.id.tvTabPhoto_rlBottom_activity_publish_video)
    TextView mTvTabPhoto;
    @BindView(R.id.tvTabCamera_rlBottom_activity_publish_video)
    TextView mTvTabCamera;

    private PublishVideoFromPhotoFragment mPhotoFragment;
    private PublishVideoFromCameraFragment mCameraFragment;

    @Override
    public int getLayoutId(Bundle bundle) {
        //requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
        return R.layout.activity_publish_video;
    }

//    @Override
//    public boolean useWindowBackground() {
//        return false;
//    }

    /**
     * 取消侧滑关闭页面（两个Tab都包含SurfaceView，切换Tab的时候会透下去看到上一个页面的内容，所以把该页面的主题这是成了背景不透明，背景都不透明了就没必要侧滑关闭了）
     *
     * @return
     */
    @Override
    public boolean useSwipeFinish() {
        return false;
    }

    @Override
    public boolean useLightMode() {
        return false;
    }

    /**
     * 关闭页面，不使用动画效果
     *
     * @param isAnim
     */
    @Override
    public void exit(boolean isAnim) {
        super.exit(false);
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        mRlBottom.setKeepScreenOn(true);
        // 默认切换到拍摄
        onClickTabCamera();
    }

    /**
     * tab选中项
     *
     * @return 0相册、1拍摄
     */
    public int getTabPosition() {
        return mTvTabPhoto.isSelected() ? 0 : 1;
    }

    @Override
    public void initPresenter() {
        mPresenter.initPresenter(this);
    }

    @OnClick(R.id.tvTabPhoto_rlBottom_activity_publish_video)
    public void onClickTabPhoto() {
        mTvTabPhoto.setSelected(true);
        mTvTabCamera.setSelected(false);
        mRlBottom.setBackgroundColor(ContextCompat.getColor(this, R.color.black));

        RxBus.get().post(PublishVideoFromCameraFragment.TAG, "onPause");
        RxBus.get().post(PublishVideoFromPhotoFragment.TAG, "onResume");

        changeFragment(0);
    }

    @OnClick(R.id.tvTabCamera_rlBottom_activity_publish_video)
    public void onClickTabCamera() {
        mTvTabPhoto.setSelected(false);
        mTvTabCamera.setSelected(true);
        mRlBottom.setBackgroundColor(ContextCompat.getColor(this, R.color.transparent));

        RxBus.get().post(PublishVideoFromCameraFragment.TAG, "onResume");
        RxBus.get().post(PublishVideoFromPhotoFragment.TAG, "onPause");

        changeFragment(1);
    }

    /**
     * 切換Fragment
     *
     * @param position 0显示相册，1显示拍摄
     */
    public void changeFragment(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (position == 0 && mPhotoFragment == null) {
            mPhotoFragment = PublishVideoFromPhotoFragment.newInstance();
            ft.add(R.id.flContainer_activity_publish_video, mPhotoFragment);

        } else if (position == 1 && mCameraFragment == null) {
            mCameraFragment = PublishVideoFromCameraFragment.newInstance();
            ft.add(R.id.flContainer_activity_publish_video, mCameraFragment);
        }

        Fragment showFragment = position == 0 ? mPhotoFragment : mCameraFragment;
        Fragment hideFragment = position == 0 ? mCameraFragment : mPhotoFragment;
        ft.show(showFragment);

        if (hideFragment != null) {
            ft.hide(hideFragment);
        }
        ft.commit();
    }
}
